package com.flycent.hqhs.ui;

import com.flycent.hqhs.ui.view.MyPanoramicView;
import com.flycent.hqhs.util.ImageUtil;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.Window;

/**
 * @author fbl
 * 720 全景展示页面
 * 传入图片的路径 path
 */
public class PanoramicShowViewActivity extends BaseActivity{

	GLSurfaceView mGlSurfaceView;
	MyPanoramicView mBall;
	private float mPreviousY;
	private float mPreviousX;
	private Bitmap bitmap;
	private String path ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		path = getIntent().getStringExtra("path");
		showLoadingDialog("");
		new Thread(){
			public void run() {
				bitmap = ImageUtil.GetLocalOrNetBitmap(path);	
				mHandler.sendEmptyMessage(0);
			};
		}.start();


		//		mGlSurfaceView = new GLSurfaceView(this);
		//		mGlSurfaceView.setEGLContextClientVersion(2);
		//		
		//		mBall = new Ball(this,id);
		//		mGlSurfaceView.setRenderer(mBall);
		//		setContentView(mGlSurfaceView);
	}

	@Override
	public boolean onTouchEvent(MotionEvent e) {
		float y = e.getY();
		float x = e.getX();
		switch (e.getAction()) {
		case MotionEvent.ACTION_MOVE:
			float dy = y - mPreviousY;// 计算触控笔Y位移
			float dx = x - mPreviousX;// 计算触控笔X位移
			mBall.yAngle += dx * 0.3f;// 设置填充椭圆绕y轴旋转的角度
			mBall.xAngle += dy * 0.3f;// 设置填充椭圆绕x轴旋转的角度
		}
		mPreviousY = y;// 记录触控笔位置
		mPreviousX = x;// 记录触控笔位置
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (null != mGlSurfaceView) {
			mGlSurfaceView.onResume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (null != mGlSurfaceView) {
			mGlSurfaceView.onPause();
		}
	}

	@SuppressLint("HandlerLeak") 
	final Handler mHandler = new Handler(){

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case 0:
				dismissLoadingDialog();
				mGlSurfaceView = new GLSurfaceView(PanoramicShowViewActivity.this);
				mGlSurfaceView.setEGLContextClientVersion(2);

				mBall = new MyPanoramicView(PanoramicShowViewActivity.this,bitmap);
				mGlSurfaceView.setRenderer(mBall);
				setContentView(mGlSurfaceView);
				break;

			default:
				break;
			}
		}

	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		dismissLoadingDialog();
	}

}
